package com.atguigu;

import io.minio.*;
import io.minio.errors.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

//TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> icon in the gutter.
public class App {
    public static void main(String[] args) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
    final String bucketName = "b002";
       //1访问桶
        MinioClient minioClient = MinioClient.builder()
                .credentials("minioadmin","minioadmin")
                .endpoint("http://192.168.26.128:9000").build();  //1.12初始化，设置用户名，密码、、管理地址


        boolean flag = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());

        //2.如果桶不存在则创建，并设置策略
        if (!flag){
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());//创建桶

            //策略
            String policy= """
                    {
                        "Version": "2012-10-17",
                        "Statement": [
                            {
                                "Effect": "Allow",
                                "Principal": {
                                    "AWS": [
                                        "*"
                                    ]
                                },
                                "Action": [
                                    "s3:GetObject"
                                ],
                                "Resource": [
                                    "arn:aws:s3:::b002/*"
                                ]
                            }
                        ]
                    }
                    """;
            minioClient.setBucketPolicy(SetBucketPolicyArgs.builder()
                    .bucket(bucketName)
                    .config(policy).build());//设置自定义代码桶
        }

        //3.传图
        minioClient.uploadObject(UploadObjectArgs.builder()
                        .bucket(bucketName)
                .object("公寓-健身房.jpg")
                .filename("F:\\BaiduNetdiskDownload\\04 尚庭公寓+git\\项目课件\\11.新项目一尚庭公寓\\资料\\图片资源\\7.images\\公寓-健身房.jpg").build());

    }
}